#furno float control
#include <LiquidCrystal.h>
//Pin assignments
int lim1pin=0;
int lim2pin=1;
int RS=2;
int EN=3;
int DB4=4;
int DB5=5;
int DB6=6;
int DB7=7;
int compressorPin=8;
int carPistonPin=9;
int sharkPistonPin=10;
int rocketPin=11;
int pressurePin=12;
int enablePin=13;

//Variable assignments
int enableState=0;      //checks enable switch
int lim1state=0;        //checks limit switch 1
int lim2state=0;        //checks limit switch 2
int pressureState=0;    //checks high/low pressure switch
int compressorPower=0;  //value for compressor relay
int carPower=0;         //value for car solenoid relay
int sharkPower=0;       //value for shark solenoid relay
int rocketSpeed=127;    //value for rocket ESC
unsigned int disableTime = 0; //used to reduce LCD writes in disabled mode

LiquidCrystal lcd(RS, EN, DB4, DB5, DB6, DB7);

void setup() {


  //setting correct pin modes
  pinMode(0,INPUT);
  pinMode(1,INPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);

  // initialize all devices
  pressureState = digitalRead(pressurePin);
  lim1state = digitalRead(lim1pin);
  lim2state = digitalRead(lim2pin);
  enableState = digitalRead(enablePin);
  digitalWrite(compressorPin, compressorPower);
  digitalWrite(carPistonPin, carPower);
  digitalWrite(sharkPistonPin, sharkPower);
  analogWrite(rocketPin, rocketSpeed);
  //initialize LCD, begin LCD opening sequence
  lcd.begin(16, 2); //16x2 LCD
  lcd.setCursor(0,0); 
  lcd.print("MIKE'S ANIMATION");
  lcd.setCursor(0,1);
  lcd.print("CONTROLLER V 2.0");
  delay(5000);
  lcd.clear();
  lcd.setCursor(0,0); //Animated Loading Screen
  lcd.print("Loading");
  for(int x = 7; x < 15; x++) 
  {
    lcd.setCursor(x, 0);
    lcd.print(".");
    delay(750);  
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SYSTEM READY!");
  delay(1500);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("WARNING! INITIAL");
  lcd.setCursor(0,1);
  lcd.print("CHARGE SEQUENCE");
  pressureState = digitalRead(pressurePin); //read the pressure pin once more
  while(pressureState == 1) //go into initial charging before running loop code
  {
    pressureState = digitalRead(pressurePin);
    compressorPower = 1;
    digitalWrite(compressorPin, compressorPower); 
  }
  compressorPower = 0;
  digitalWrite(compressorPin, compressorPower);
}

void loop() 
{
  //check sensors
  pressureState = digitalRead(pressurePin);
  lim1state = digitalRead(lim1pin);
  lim2state = digitalRead(lim2pin);
  enableState = digitalRead(enablePin);

  while(enableState == 0) //while the enable switch is OFF
  {
    enableState = digitalRead(enablePin); //always keep checking enable switch
    //stop everything
    digitalWrite(compressorPin, 0);
    digitalWrite(carPistonPin, 0);
    digitalWrite(sharkPistonPin, 0);
    analogWrite(rocketPin, 127);
    if(disableTime == 0)
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("DISABLED. FLIP");
      lcd.setCursor(0,1);
      lcd.print("SWITCH TO ENABLE");
     disableTime++; 
    }
    
  }
}





